package com.rabbit.controller;

import com.github.pagehelper.PageInfo;
import com.rabbit.annotation.Log;
import com.rabbit.annotation.LoginUser;
import com.rabbit.constant.OperType;
import com.rabbit.db.entity.User;
import com.rabbit.exception.BusinessException;
import com.rabbit.service.UserService;
import com.rabbit.util.ResultGenUtil;
import com.rabbit.validate.base.*;
import com.rabbit.vo.ResultVO;
import com.rabbit.vo.req.UserReq;
import com.rabbit.vo.res.UserRes;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <h1>用户管理</h1>
 *
 * @author zjw
 * @date 2022/1/14 15:40
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户列表
     */
    @PostMapping("/list")
    public ResultVO<Object> list(@RequestBody UserReq userReq) {
        PageInfo<UserRes> pageInfo = userService.queryUserList(userReq);
        return ResultGenUtil.genSuccess(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 基本资料修改
     */
    @PostMapping("/updateBaseInfo")
    public ResultVO<Object> updateBaseInfo(@LoginUser User loginUser, @RequestBody UserReq userReq) throws BusinessException {
        userReq.setUserId(loginUser.getUserId());
        userService.checkUnique(userReq, 2);
        userReq.setOperatorUserName(loginUser.getUserName());
        userService.updateBaseInfo(userReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 删除用户
     */
    @PostMapping("/delete")
    @Log(name = "用户管理", type = OperType.DELETE)
    public ResultVO<Object> deleteUser(@Validated(Delete.class) @RequestBody UserReq userReq) throws BusinessException {
        userService.checkIsAdmin(userReq.getUserId());
        userService.deleteUser(userReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 重置密码
     */
    @PostMapping("/resetPassword")
    @Log(name = "用户管理", type = OperType.UPDATE)
    public ResultVO<Object> resetPassword(@LoginUser User loginUser, @Validated(UpdatePwd.class) @RequestBody UserReq userReq) throws BusinessException {
        userService.checkIsAdmin(userReq.getUserId());
        userReq.setOperatorUserName(loginUser.getUserName());
        userService.resetPassword(userReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 新增用户
     */
    @PostMapping("/add")
    @Log(name = "用户管理", type = OperType.INSERT)
    public ResultVO<Object> addUser(@LoginUser User loginUser, @Validated(Insert.class) @RequestBody UserReq userReq) throws BusinessException {
        userService.checkUnique(userReq, 1);
        userReq.setOperatorUserName(loginUser.getUserName());
        userService.addUser(userReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 修改用户
     */
    @PostMapping("/update")
    @Log(name = "用户管理", type = OperType.UPDATE)
    public ResultVO<Object> updateUser(@LoginUser User loginUser, @Validated(Update.class) @RequestBody UserReq userReq) throws BusinessException {
        userService.checkIsAdmin(userReq.getUserId());
        userService.checkUnique(userReq, 2);
        userReq.setOperatorUserName(loginUser.getUserName());
        userService.updateUser(userReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 查询指定用户
     */
    @GetMapping("/queryUserById")
    public ResultVO<Object> queryUserById(@RequestParam Long userId) throws BusinessException {
        // 检查是否越权访问
        userService.checkDataScope(userId);
        return ResultGenUtil.genSuccess(userService.queryUserById(userId));
    }

    /**
     * 调整账号状态
     */
    @PostMapping("/changeEnabled")
    @Log(name = "用户管理", type = OperType.UPDATE)
    public ResultVO<Object> changeEnabled(@LoginUser User loginUser, @Validated(Enable.class) @RequestBody UserReq userReq) throws BusinessException {
        userService.checkIsAdmin(userReq.getUserId());
        userReq.setOperatorUserName(loginUser.getUserName());
        userService.updateUserEnabled(userReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 更新用户头像
     */
    @PostMapping("/updateAvatar")
    public ResultVO<Object> updateAvatar(@LoginUser User loginUser, @RequestBody UserReq userReq) throws BusinessException {
        userReq.setUserId(loginUser.getUserId());
        userReq.setOperatorUserName(loginUser.getUserName());
        String fileUrl = userService.updateAvatar(userReq);
        return ResultGenUtil.genSuccess(fileUrl);
    }
}